﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace AISFMIOperator
{
    public partial class CityForm : Form
    {
        public CityForm()
        {
            InitializeComponent();
            RefreshAllData();
        }

        private void RefreshAfterAdd()
        {
            AISFMIOperator.Region r = (AISFMIOperator.Region)region.SelectedItem;

            if (r != null)
            {
                cityTable.DataSource = City.GetAllCities().Select(o => o.Region.RegionID == r.RegionID);
                cityTable.Columns[0].DataPropertyName = "CityName";
            }
        }
        

        private void RefreshAllData()
        {
            region.DataSource = AISFMIOperator.Region.GetAllRegions();
            region.DisplayMember = "RegionName";

            AISFMIOperator.Region r = (AISFMIOperator.Region)region.SelectedItem;

            if (r != null)
            {
                cityTable.AutoGenerateColumns = false;
                cityTable.DataSource = City.GetAllCities().Where(o => o.Region.RegionID == r.RegionID).ToList();
                cityTable.Columns[0].DataPropertyName = "CityName";
            }
        }

        private void CityForm_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            AddRegionForm arf = new AddRegionForm();
            arf.ShowDialog();
            RefreshAllData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (region.SelectedItem != null)
            {
                AddCityForm ac = new AddCityForm((AISFMIOperator.Region)region.SelectedItem);
                ac.ShowDialog();
                RefreshAllData();
            }
        }
    }
}
